package com.stylefeng.guns.modular.borrowing.controller;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.aop.Platform;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.borrowing.service.IBorrowingInfoService;
import com.stylefeng.guns.modular.system.model.BorrowingInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 应收应付汇总报表控制器
 *
 * @author fengshuonan
 * @Date 2018-07-04 11:24:01
 */
@Controller
@RequestMapping("/borrowing")
public class BorrowingController extends BaseController {

    private String PREFIX = "/borrowing/borrowing/";

    @Autowired
    private IBorrowingInfoService borrowingInfoService;



    /**
     * 跳转到应收应付报表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "borrowing.html";
    }





    /**
     * 获取应收应付汇总报表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@Platform String platform,@RequestParam(required = false) String beginDate, @RequestParam(required = false) String  endDate,@Platform String contact) throws Exception {

        if(!StringUtils.isNotBlank(beginDate)) {
            beginDate=DateUtil.today(0);
        }
        if(!StringUtils.isNotBlank(endDate)) {
            endDate= DateUtil.today(1);
        }
        return borrowingInfoService.getBorrowing(contact,platform,beginDate,endDate);
    }




    /**
     * 描述:导出应收应付
     * 创建人: laoyu
     * 日期: 2018/8/6 0006
     * 时间: 下午 5:04
     */
    @RequestMapping("/load")
    public void downloadByPoiBaseView(ModelMap modelMap, HttpServletRequest request,
                                      HttpServletResponse response, @RequestParam(required = true)String beginDate
                                                                    ,@RequestParam(required = true)String endDate) throws Exception  {


        //查询要导出工资
        if (org.apache.commons.lang.StringUtils.isBlank(beginDate)){
            throw new GunsException(BizExceptionEnum.BUSINSS_ERROR,"请选择要导出开始时间");
        }
        if (org.apache.commons.lang.StringUtils.isBlank(endDate)){
            throw new GunsException(BizExceptionEnum.BUSINSS_ERROR,"请选择要导出结束时间");
        }
        EntityWrapper<BorrowingInfo> wrapper = new EntityWrapper<BorrowingInfo>();

        //组装查询条件
        if(StringUtils.isNotBlank(beginDate)) {
            wrapper.ge("rev_date", beginDate);
        }else{
            wrapper.ge("rev_date", DateUtil.today(0));
        }
        if(StringUtils.isNotBlank(endDate)) {
            wrapper.le("rev_date", endDate);
        }else{
            wrapper.le("rev_date", DateUtil.today(1));
        }
        String platform = ShiroKit.getUser().getDeptcode();

        //初始化导出的模板表格
        TemplateExportParams params = new TemplateExportParams(
                "exceldoc/borrowing.xlsx");

        modelMap.put(TemplateExcelConstants.FILE_NAME, platform+beginDate+"--"+endDate+"应收应付余额明细汇总表");
        modelMap.put(TemplateExcelConstants.PARAMS, params);
        modelMap.put(TemplateExcelConstants.MAP_DATA,  borrowingInfoService.loadBorrowing(wrapper,platform,beginDate,endDate));
        PoiBaseView.render(modelMap, request, response,
                TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);

    }

    /**
     * 描述:应付应收合计
     * 创建人: xiang
     * 日期: 2018/8/9
     * 时间: 10:13
     */
    @RequestMapping(value = "/tatol")
    @ResponseBody
    public Object total(@Platform String platform,@RequestParam(required = false) Integer type, @RequestParam(required = false) String beginDate, @RequestParam(required = false) String  endDate) throws Exception {
        //初始化日期
        if(!StringUtils.isNotBlank(beginDate)) {
            beginDate=DateUtil.today(0);
        }
        if(!StringUtils.isNotBlank(endDate)) {
            endDate= DateUtil.today(1);
        }
        Object object=borrowingInfoService.tatol(type,beginDate,endDate,platform);
        return object;
    }

  


}
